using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TestWebApi.IServices;
using TestWebApi.Util;

namespace TestWebApi.Controller;

[ApiController]
[Route("api/[controller]/[action]")]
public class TestController : ControllerBase
{
    public IDataService _IdataService;
    
    
    public TestController(IDataService idataService)
    {
        _IdataService = idataService;
    }
    [HttpGet]
    [Authorize]
    public IActionResult Index()
    {
        //将字符串“你好” 格式化 json
        return Ok(new { message = "你好" });
    }
    
    [HttpGet]
    [Authorize]
    public IActionResult GetData(string connectionName,string tableName,string startTime,string endTime)
    {
        //格式化开始和结束时间为DateTime
        try
        {
            AppLogger.Information("正在获取Mysql数据开始时间：{0} 结束时间：{1}",startTime,endTime);
            var parseStatTime = DateTime.Parse(startTime);
            var parseEndTime = DateTime.Parse(endTime);
            var resultData=  _IdataService.GetMySqlData(connectionName,tableName,parseStatTime,parseEndTime);
            AppLogger.Information($"获取Mysql数据成功，接口名称--GetData，完成时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}");
            return Ok(resultData);
        }
        catch (Exception e)
        {
            AppLogger.Information($"日期格式转换错误,异常出现时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}");
            return BadRequest("日期格式转换错误，示例：2023-05-01 00:00:00");
        }
    }
    [HttpGet]
    [Authorize]
    public IActionResult GetDataByDynamic(string connectionName,string tableName,string startTime,string endTime)
    { 
        try
        {
            AppLogger.Information("正在获取Mysql数据开始时间：{0} 结束时间：{1}",startTime,endTime);
            var parseStatTime = DateTime.Parse(startTime);
            var parseEndTime = DateTime.Parse(endTime);
            var resultData=  _IdataService.GetMySqlDataByDynamic(connectionName,tableName,parseStatTime,parseEndTime);
            AppLogger.Information($"获取Mysql数据成功，接口名称--GetDataByDynamic，完成时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}");
            return Ok(resultData);
          
        }
        catch (Exception e)
        {
            AppLogger.Information($"日期格式转换错误,异常出现时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}");
            return BadRequest("日期格式转换错误，示例：2023-05-01 00:00:00");
        }
    }
    
}